import { BasePage, storage } from "../../common/index";
import DetailService from "../../services/detail";
export default class DetailController extends BasePage {
  constructor(data) {
    super(data);
    this._registerPage(DetailController);
    this.searchService = new DetailService();
    this.storage = storage;
  }

  /**
   * 生命周期函数
   */
  onLoad(options) {
    this.data.id = this.storage.data.currentBookDetail.id;
    this.data.book = this.storage.data.currentBookDetail;
    this.data.isLoadChapter = false;
    this.initIsHaveBooksrack();
    this.formatData();
    this.searchResource((data) => {
      this.searchDetail(
        {
          type: 2,
          id: this.data.book.sourceBookId,
        },
        data
      );
    });
  }
  initIsHaveBooksrack() {
    const bookList = this.storage.data.bookList || [];
    this.data.isHaveBooksrack = bookList.find(
      (book) => book.id === this.data.id
    );
    this.data.isHaveBooksrack = !!this.data.isHaveBooksrack;
    console.log(this.data.isHaveBooksrack);
  }
  formatData() {
    const book = this.data.book;
    let dataAuthor = book.author.split("：");
    if (dataAuthor.length != 2) {
      dataAuthor = book.author.split(":");
    }
    if (dataAuthor.length != 2) {
      book.author = dataAuthor[0];
    } else {
      book.author = dataAuthor[1];
    }
    this.data.book = book;
  }
  /**
   * 加入书架
   */
  addBooksrack() {
    const bookList = this.storage.data.bookList || [];
    const book = bookList.find((book) => book.id === this.data.id);
    if (!book) {
      bookList.push(this.data.book);
    }
    this.storage.data.bookList = bookList;
    this.initIsHaveBooksrack();
    wx.showToast({
      title: "已加入书架",
      icon: "success",
      duration: 1000,
    });
  }

  /**
   * 移除书架
   */
  deleteBooksrack() {
    const result = [];
    const bookList = this.storage.data.bookList || [];
    bookList.map((item) => {
      if (item.id != this.data.book.id) {
        result.push(item);
      }
    });
    this.storage.data.bookList = result;
    this.initIsHaveBooksrack();
    wx.showToast({
      title: "已移除书架",
      icon: "success",
      duration: 1000,
    });
  }

  /**
   * 开始阅读
   */
  startRead() {
    this.storage.data.currentBookChapter = this.data.book.chapter[0];
    this.storage.data.currentBookDetail = this.data.book;
    wx.navigateTo({
      url: "/pages/content/content",
    });
  }

  /**
   * 初始化方法
   */
  searchResource(callback) {
    this.searchService
      .searchRes({
        id: this.data.id,
        type: 1,
      })
      .then((data) => {
        if (data.length === 0) {
          this.searchService
            .searchRes({
              id: this.data.id,
              type: 2,
            })
            .then((res) => {
              callback(res);
            });
        } else {
          callback(data);
        }
      });
  }

  /**
   * 获得小说详情
   * @param {String} id 书籍ID
   */
  searchDetail(params, sources) {
    this.searchService.search(params).then((res) => {
      this.data.isLoadChapter = true;
      res.sources = sources;
      res.currentSource = sources.find(
        (item) => item.id == this.data.book.sourceBookId
      );
      this.data.book = res;
      this.formatData();
      if (this.data.isHaveBooksrack) {
        const bookList = this.storage.data.bookList || [];
        bookList.map((item) => {
          if (item.id == this.data.book.id) {
            this.data.book.currentChapter = item.currentChapter || 0;
            item = this.data.book;
          }
        });
        this.storage.data.bookList = bookList;
      }
      if (!this.data.book.lastChapter.name) {
        const book = this.data.book;
        book.lastChapter = book.chapter[book.chapter.length - 1];
        this.data.book = book;
      }
    });
  }
}

new DetailController({
  isLoadChapter: false,
  isHaveBooksrack: false,
  id: null,
  book: {},
});
